// pages/portfolio/portfolio.js
// 模拟数据
const mockStocks = [
  { code: '600000', name: '浦发银行', price: 10.25, change: 0.52, open: 10.10, high: 10.30, low: 10.05, volume: 150000 },
  { code: '000001', name: '平安银行', price: 15.30, change: -1.23, open: 15.40, high: 15.45, low: 15.10, volume: 220000 },
  { code: '601318', name: '中国平安', price: 48.75, change: 2.15, open: 48.00, high: 48.90, low: 47.80, volume: 350000 },
  { code: '600519', name: '贵州茅台', price: 1720.50, change: 1.85, open: 1710.00, high: 1725.00, low: 1705.00, volume: 50000 }
];

// 用户账户数据
let account = {
  balance: 1000000,
  positions: [
    { code: '600000', name: '浦发银行', amount: 1000, cost: 10.00 },
    { code: '601318', name: '中国平安', amount: 500, cost: 48.50 }
  ]
};

Page({
  data: {
    portfolioData: []
  },

  onShow() {
    this.renderPortfolioTable();
  },

  renderPortfolioTable() {
    if (account.positions.length === 0) {
      this.setData({
        portfolioData: []
      });
      return;
    }

    const portfolioData = account.positions.map(position => {
      const stock = mockStocks.find(s => s.code === position.code);
      const currentPrice = stock ? stock.price : position.cost;
      const profit = (currentPrice - position.cost) * position.amount;
      const profitPercent = ((currentPrice / position.cost) - 1) * 100;
      
      return {
        code: position.code,
        name: position.name,
        amount: position.amount,
        cost: position.cost.toFixed(2),
        currentPrice: currentPrice.toFixed(2),
        profit: profit.toFixed(2),
        profitPercent: profitPercent.toFixed(2),
        profitClass: profit >= 0 ? 'text-red-500' : 'text-green-500'
      };
    });

    this.setData({
      portfolioData: portfolioData
    });
  }
});